<div class="layui-container" style="padding: 5%">
    <div class="layui-row">
        <div class="layui-col-lg8 layui-col-lg-offset2">
            <form class="layui-form" id="teacher-form">

                <div class="layui-form-item">
                    <label class="layui-form-label">教师姓名</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" autocomplete="off" lay-verify="name" placeholder="请输入姓名"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">单选框</label>
                    <div class="layui-input-block">
                        <input type="radio" name="gender" value="0" title="男" lay-verify="gender"
                               checked="">
                        <input type="radio" name="gender" value="1" title="女" lay-verify="gender">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">手机</label>
                        <div class="layui-input-inline">
                            <input type="text" name="phone" lay-verify="phone" autocomplete="off"
                                   class="layui-input">
                        </div>
                    </div>

                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">邮箱</label>
                        <div class="layui-input-inline">
                            <input type="text" name="email" lay-verify="email" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">院系</label>
                    <div class="layui-input-inline">
                        <select name="collegeId" id="select-collegeInfo" type="select" lay-verify="collegeId"
                                lay-filter="select-collegeInfo">
                            <option value="">请选择学院</option>
                        </select>
                    </div>
                    <div class="layui-input-inline">
                        <select name="systemId" id="select-system" lay-verify="systemId">
                            <option value="">请选择系</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button type="submit" class="layui-btn" lay-submit lay-filter="demo1" id="sub-btn">立即提交</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>


<script>
    layui.use(['form', 'layedit', 'laydate'], function () {
        var form = layui.form
            , layer = layui.layer;
        // 页面加载函数
        $(function () {
            // 获取所有的学院信息
            $.ajax({
                url: "info/college",
                type: "GET",
                dataType: "JSON",
                success: function (JSONResult) {
                    let code = JSONResult.code;
                    let data = JSONResult.data;
                    if (code === 0) {
                        data.forEach((item) => $("#select-collegeInfo").append("<option value=" + item.collegeId + ">" + item.name + "</option>"))
                        form.render();
                    }
                }
            })

        })
        form.on('radio', function () {
            $(this).attr("checked", "checked");
            form.render('radio');
        })
        form.on('select(select-collegeInfo)', function (data) {
            let value = data.value;
            $.ajax({
                url: "info/system",
                type: "GET",
                data: {collegeId: value},
                dataType: "JSON",
                success: function (JSONResult) {
                    let code = JSONResult.code;
                    let data = JSONResult.data;
                    if (code === 0) {
                        $("#select-system").empty();
                        $("#select-system").append("<option value=>请选择系</option>");
                        data.forEach((item) => $("#select-system").append("<option value=" + item.systemId + ">" + item.name + "</option>"))
                        form.render();
                    }
                }
            })
        });
        form.verify({

            name: function (value) {
                if (value.length < 1) {
                    return '姓名不能为空！';
                }
            },
            collegeId: function (value) {
                if (value.length < 1) {
                    return '请选择学院!';
                }
            },
            systemId: function (value) {
                if (value.length < 1) {
                    return '请选择系!';
                }
            }
        });
        //监听提交
        form.on('submit(demo1)', function (data) {
            // submit 提交事件
            let serialize = $("#teacher-form").serialize();
            $.ajax({
                url: "/teacher/add",
                type: "POST",
                data: serialize,
                dataType: "JSON",
                success: function (JSONResult) {
                    if (JSONResult.code === 0) {
                        layer.msg("添加完成！");
                    } else {
                        layer.msg("添加失败！");
                    }
                }
            });
            return false;
        });
    })

</script>